package 补充.多线程;
class MySingleton2{
    private static MySingleton2 instance;
    private MySingleton2(){
    }
    public static MySingleton2 getInstance() throws InterruptedException {
        try {
            Thread.sleep(1000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
        if(instance == null){
            instance = new MySingleton2();
        }
        return instance;
    }
    public void show(){
        System.out.println("懒汉式单列模式");
    }
}
public class 懒汉式单列模式 {
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            Thread thread=new Thread(()->{
                try {
                    System.out.println(MySingleton2.getInstance().hashCode());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
            thread.start();
            System.out.println(MySingleton2.getInstance().hashCode());
        }
    }
}
